package me.ywy.simplelauncher.receiver;

import me.ywy.simplelauncher.base.AppList;
import me.ywy.simplelauncher.home.HomePageActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class LauncherReceiver extends BroadcastReceiver {

	private static LauncherReceiver instance;
	private IntentFilter filter = null;

	public static LauncherReceiver getInstance() {
		if (instance == null) {
			instance = new LauncherReceiver();
		}
		return instance;
	}

	public LauncherReceiver() {
		super();
		filter = new IntentFilter();
		filter.addAction(Intent.ACTION_PACKAGE_ADDED);
		filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
		filter.addDataScheme("package");
	}

	public void onReceive(Context arg0, Intent intent) {
		if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {     // install
			String pkg = intent.getDataString().replace("package:", "");
			HomePageActivity.getInstance().showToast("Install:" + pkg);
			AppList.getInstance().addApp(pkg);
			AppList.getInstance().srotList();
			HomePageActivity.getInstance().reflushAPP();
		}
		if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {   // uninstall
			String pkg = intent.getDataString().replace("package:", "");
			HomePageActivity.getInstance().showToast("uninstall:" + pkg);
			AppList.getInstance().removeApp(pkg);
			HomePageActivity.getInstance().reflushAPP();
		}
	}

	public IntentFilter getFilter() {
		return filter;
	}

}
